<template>
	<div class="formBox">
		<div class="container">
			<div class="title">
				{{formInfo.title}}
			</div>
			<div class="inpBox" v-for="(inp,j) in formInfo.form" :key="j">
				<div class="laebl">{{j + 1}}、{{ inp.title }}</div>
				<div class="inp" v-if="inp.status == 'rate'">
					<u-rate count="10" v-model="inp.answer"></u-rate>
				</div>
				<div class="inp" v-if="inp.status == 'options'">
					<u-radio-group v-model="inp.answer" placement="column" >
						<u-radio :customStyle="{marginBottom: '8px'}" v-for="(item, k) in inp.options" :key="k" :label="item" :name="item"></u-radio>
					</u-radio-group>
				</div>
				<div class="inp" v-if="inp.status == 'input'">
					<u--input :placeholder="'请输入' + inp.title" border="surround" v-model="inp.answer"></u--input>
				</div>
			</div>
		</div>
		<div class="fixBottom">
			<div class="container">
				<div class="btn" v-if="!outTime">
					<u-button type="error" color="#d9232f" shape="circle" text="立即提交" @click="subForm" v-if="!ansStatus"></u-button>
					<u-button type="error" color="#d9232f" shape="circle" text="您已提交" :disabled="true" v-else></u-button>
				</div>
				<div class="btn" v-else>
					<u-button type="error" color="#d9232f" block disabled="true">不在提交时间内</u-button>
				</div>
			</div>
		</div>
		<div class="loadBox" v-if="showLoading">
			<u-loading-icon size="36" mode="circle" :vertical="true" color="red" text="加载中..." textSize="20"></u-loading-icon>
		</div>
		<div class="loadBox" v-if="showLoading">
			<u-loading-icon size="36" mode="circle" :vertical="true" color="red" text="加载中..." textSize="20"></u-loading-icon>
		</div>
	</div>
</template>

<script>
	const db = wx.cloud.database()
export default {
	name: 'headcomd',
	data() {
		return {
			hdid:"",
			formInfo:{},
			openid:"",
			showLoading:true,
			ansStatus:false,
			outTime:false
		};
	},
	onShareAppMessage(options) {},
	onLoad(options) {
		if (!options) {
			this.infoScene = 'default';
		} else {
			this.hdid = options.hdid
			this.getopenid()
		}
	},
	computed: {
		userphone() {
			return this.$store.getters.userphone ? this.$store.getters.userphone : '';
		},
	},
	mounted() {
		
	},
	watch: {
		
	},
	created() {
		let that = this;
		
	},
	methods: {
		getopenid() {
			let that = this
			that.showLoading = true
			wx.cloud.callFunction({
				name: 'getopenid',
				data: {},
				complete: res => {
					that.openid = res.result.openid
					that.getForminfo()
				}
			})
		},
		getForminfo(){
			let that = this
			db.collection('jy_zbkyy').doc(that.hdid).get({
				success:function(res){
					that.formInfo = res.data 
					that.getAnswer()
				}
			})
		},
		async getAnswer(){
			let that = this
			let ans = await db.collection("jy_zbkyy_subForm").where({_openid:that.openid,hdid:that.hdid}).get()
			if(ans.data.length > 0){
				that.formInfo.form = ans.data[0].formAnswer.form
				that.ansStatus = true
			}
			that.checkTime()
			that.showLoading = false
		},
		checkTime(){
			let that = this 
			const time = new Date().getTime() / 1000
			const sTime = new Date(that.formInfo.startTime).getTime() / 1000
			const eTime = new Date(that.formInfo.endTime).getTime() / 1000
			const tTime = new Date('2024-06-03 11:00:00').getTime() / 1000
			if(time < sTime || time > eTime){
				that.outTime = true
			}else{
				that.outTime = false
			}
		},
		async subForm(){
			let that = this
			
			that.showLoading = true
			let status = that.checkForm()
			if(!status){
				uni.showToast({
					icon:"error",
					title:'信息未填全'
				})
				that.showLoading = false
				return
			}
			
			db.collection("jy_zbkyy_subForm").add({
				data:{
					formAnswer: that.formInfo,
					hdid: that.hdid,
					time:that.getTime()
				},
				success: res =>{
					uni.showToast({
						icon:"success",
						title:"预约成功！"
					})
					that.getAnswer()
				},
				fail:req => {
					that.showLoading = false
				}
			})
			
		},
		getTime(){
			let that = this
			let day = new Date()
			let y = day.getFullYear()
			let m = (day.getMonth() + 1).toString().padStart(2, '0');
			let d = day.getDate().toString().padStart(2, '0');
			let hour = day.getHours().toString().padStart(2, '0');
			let miu = day.getMinutes().toString().padStart(2, '0');
			let time = y + '-' + m + '-' + d + ' ' + hour + ':' + miu
			return time
		},
		checkForm(){
			let that = this
			let answer = that.formInfo.form.filter(answer => answer.answer == "")
			if(answer.length > 0){
				return false
			}else{
				return true
			}
			
		},
		
	}
};
</script>

<style>
@import url(./index.css);
</style>
